package cn.tedu.micro.demo.all.service;

import cn.tedu.micro.demo.all.dao.repository.CartRepository;
import cn.tedu.micro.demo.pojo.po.CartPO;
import cn.tedu.micro.demo.pojo.vo.CartVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class CartServiceImpl implements CartService{
    @Autowired
    private CartRepository cartRepository;

    @Override
    public List<CartVO> listCart(String userId) {
        List<CartPO> cartPOList = cartRepository.findByUserId(userId);
        //转化成listvo list stream map filter sorted  forEach
        if (cartPOList!=null){

            List<CartVO> cartVOList =cartPOList.stream().map(cartPO -> {
                CartVO cartVO = new CartVO();
                cartVO.setId(cartPO.getId());
                cartVO.setUserId(cartPO.getUserId());
                cartVO.setProductCode(cartPO.getProductCode());
                cartVO.setCount(cartPO.getCount());
                return cartVO;
            }).collect(Collectors.toList());
            return cartVOList;
        }
        return Collections.emptyList();
    }
}
